<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="//apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">

        $(function () {
//            $.get("http://www.qb5.com/1_1546/599640.html", function (data) {
//                // console.log(data);
//                show(data);
//            });
//            $.ajax({
//                type: "GET",
//                url: "http://www.qb5.com/1_1546/599640.html",
//                dataType: "html",
//                headers:{
//                    "Access-Control-Allow-Origin":"http://www.qb5.com/"
//                },
//                cache: false,//是否缓存
//                beforeSend: function() {
//                    //请求后,还没收到数据...显示jquery加载相关数据
//                    $("#regResult").html('加载中')
//                },
//                success: function(data) {
//                    //jquery请求成功返回数据
//                    show(data);
//                }
//            });
            $("#ifr").load('http://www.qb5.com/1_1546/599640.html');
            setTimeout(function(){
                console.log($("#ifr").html());
//                show($("#ifr").html());
            },2000);
        });

        function show(bookPage) {
            var bodyRegex = new RegExp("<body[^>]+>[^]*?<\/body>");
            bookPage = bookPage.match(bodyRegex) + "";
            var scriptRegex = new RegExp("<script[^>]*?>[^]*?<\/script>", "gi");
            bookPage = bookPage.replace(scriptRegex, "");
            var aRegex = new RegExp("<a[^>]*?>.*?<\/a>", "gi");
            bookPage = bookPage.replace(aRegex, "");
            var liRegex = new RegExp("<li[^>]*?>.*?<\/li>", "gi");
            bookPage = bookPage.replace(liRegex, "");
            var rmBody = new RegExp("<[\/]?body[^>]*>", "gi");
            bookPage = bookPage.replace(rmBody, "");
            bookPage = bookPage.replace(/&nbsp;/ig, "");
            var rmNullP = new RegExp("<([^>]*)>(&nbsp;)*<\/[^\/]*>", "gi");
            bookPage = bookPage.replace(rmNullP, "");

            // tailDiv (bookPage);
            // headDiv (bookPage);

            var index = 0;
            while ((bookPage.indexOf("<DIV") >= 0 || bookPage.indexOf("<div") >= 0) && lastNum > 0 && index < 200) {
                bookPage = rmDiv(bookPage);
                index++;
            }
            var divRegex = new RegExp("<[\/]?(div|span)[^>]*>", "gi");
            bookPage = bookPage.replace(divRegex, "");

            $("#regResult").html(bookPage);
        }

        function rmDiv(bookPage) {
            tailDiv(bookPage);
            headDiv(bookPage);
            if (lastNum >= 0 && lastNum >= 0) {
                var temp = bookPage.substring(firstNum, lastNum + 6);
                console.log("截取div：" + temp);
                console.log(temp.length);
                if (temp.length > 2000) {
                    bookPage = changeStr(bookPage, lastNum + 3, lastNum + 5, "span");
                    bookPage = changeStr(bookPage, firstNum + 2, firstNum + 4, "span");

                } else {
                    bookPage = bookPage.replace(temp, "");
                }

                firstNum = 0;
                lastNum = 1;
            }
            return bookPage;
        }

        function changeStr(allstr, start, end, changeStr) { //allstr:原始字符串，start,开始位置,end：结束位置, changeStr:改变后的字
            return allstr.substring(0, start - 1) + changeStr + allstr.substring(end, allstr.length);
        }

        var firstNum = 0;
        var lastNum = 1;
        function headDiv(bookPage) {
            var i = bookPage.indexOf("<div");
            var j = bookPage.indexOf("<DIV");
            var tempNum = 0;
            if (i >= 0 || j >= 0) {
                if (i < 0) {
                    tempNum = j;
                } else if (j < 0) {
                    tempNum = i;
                } else {
                    tempNum = i < j ? i : j;
                }
            } else {
                tempNum = -1;
            }

            if (firstNum + tempNum - 4 < lastNum && tempNum >= 0) {
                tempNum += 4;
                firstNum += tempNum;
                var temp = bookPage.substring(tempNum, bookPage.length);
                headDiv(temp);
            } else {
                firstNum -= 4;
            }

        }
        function tailDiv(bookPage) {
            var i = bookPage.indexOf("</div>");
            var j = bookPage.indexOf("</DIV>");
            if (i >= 0 || j >= 0) {
                if (i < 0) {
                    lastNum = j;
                } else if (j < 0) {
                    lastNum = i;
                } else {
                    lastNum = i > j ? j : i;
                }
            } else {
                lastNum = -1;
            }
        }
    </script>
</head>
<body>
<div id="regResult"></div>
<div id="ifr" style="display: block"></div>
</body>
</html>